package com.magicsoft.app.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.entity.AuthorizationListResp;
import com.magicsoft.app.helper.DateUtils;

/**
 * 门禁页面 申请、授权列表适配器
 * 
 * @author liqingjun
 * 
 */
public class DoorControlListAdapter extends CTHAdapter<AuthorizationListResp> {

	// true当前显示的apply列表，false当前显示authorization列表
	private boolean isApplyListShow;

	public DoorControlListAdapter(Context context,
			List<AuthorizationListResp> data) {
		super(context, data);
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		HolderViewStatic viewContent = null;
		AuthorizationListResp authorizationListResp = data.get(position);

		if (convertView == null) {

			viewContent = new HolderViewStatic();

			convertView = mInflater.inflate(R.layout.doorcontrol_list_item,
					null);

			viewContent.tv_name = (TextView) convertView
					.findViewById(R.id.tv_name);

			viewContent.linearlayout_authorization = (LinearLayout) convertView
					.findViewById(R.id.linearlayout_authorization);
			viewContent.tv_type = (TextView) convertView
					.findViewById(R.id.tv_type);
			viewContent.tv_community = (TextView) convertView
					.findViewById(R.id.tv_community);
			viewContent.tv_auth_time = (TextView) convertView
					.findViewById(R.id.tv_auth_time);
			convertView.setTag(viewContent);
		} else {

			viewContent = (HolderViewStatic) convertView.getTag();
		}

		String name = "";
		String statuString = "";

		if (isApplyListShow) {

			name = authorizationListResp.getFromname();
		} else {

			name = authorizationListResp.getToname();
		}
		
		String type = authorizationListResp.getType();
		String isdelete = authorizationListResp.getIsdeleted();
		if ("1".equals(type)) {
			viewContent.tv_community.setVisibility(View.GONE);
			// 默认 未批复
			if ("1".equals(isdelete)) {
				viewContent.tv_type.setTextColor(context.getResources()
						.getColor(R.color.apply_status_reject));
				statuString = context
						.getString(R.string.apply_status_reject);
			} else {
				// 未批复 蓝色
				viewContent.tv_type.setTextColor(context.getResources()
						.getColor(R.color.apply_status_unapprove));
				statuString = context
						.getString(R.string.apply_status_unreply);
			}
		} else if ("2".equals(type)){
			
			viewContent.tv_community.setVisibility(View.VISIBLE);
			if ("1".equals(isdelete)) {
				// 已失效
				viewContent.tv_type.setTextColor(context.getResources()
						.getColor(R.color.authorization_status_expire));
				statuString = "已失效";
			} else if ("2".equals(type)) {
				// 已批复
				String auType = authorizationListResp.getUsertype();
				// 绿色
				viewContent.tv_type.setTextColor(context.getResources()
						.getColor(R.color.apply_status_pass));
				// 默认
				if ("1".equals(auType)) {
					statuString = "永久";
				} else if ("2".equals(auType)) {
					statuString = "7天";
				} else if ("3".equals(auType)) {
					statuString = "1天";
				} else if ("4".equals(auType)) {
					statuString = "2小时";
				} else if ("5".equals(auType)) {
					statuString = "1年";
				}
			}
		}

		viewContent.tv_name.setText(name);
		viewContent.tv_type.setText(statuString);
		viewContent.tv_auth_time.setText(DateUtils.phpToString(authorizationListResp.getCreationtime(),
				DateUtils.DATE_FORMAT_MONTH_DAY_TIME));
		viewContent.tv_community.setText(authorizationListResp.getName());

		return convertView;
	}

	static class HolderViewStatic {
		// 姓名
		TextView tv_name;
		// 授权状态布局
		LinearLayout linearlayout_authorization;
		// 授权类型
		TextView tv_type;
		// 授权小区
		TextView tv_community;
		// 授权时间
		TextView tv_auth_time;

	}

	public boolean isApplyListShow() {
		return isApplyListShow;
	}

	public void setApplyListShow(boolean isApplyListShow) {
		this.isApplyListShow = isApplyListShow;
	}

}
